﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;
using Hangjing.WebCommon;

/// <summary>
///  更新订单状态（当状态为3为配送完成时，订单状态也变成完成）
// 传入参数：订单编号 配送状态状态 
/// </summary>
public partial class App_Android_Deliver_SaveOrderState : APIPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();

        string orderid = WebUtility.InputText(Request["orderid"]);
        int state = Convert.ToInt32(WebUtility.InputText(Request["state"])); //配送状态：0：未处理,1取货中，2：配送中，3：配送完成， 4：配送失败
        int did = HjNetHelper.GetQueryInt("did", 0);



        Custorder dal = new Custorder();

        string sql = "update custorder set sendstate=" + state;
        sql += " where OrderID='" + orderid + "' and deliverid=" + did;

        

        if (WebUtility.excutesql(sql) > 0)
        {
            CustorderInfo order = dal.GetModel(orderid);
            Response.Write("{\"orderid\":\"" + orderid + "\",\"state\":\"1\"}");

            //上班状态：0:下班，10：待岗，20：取货中，30：送货中，40：异常
            switch (state)
            {
                case 1:

                    {
                        if (order.OrderStatus == 1)
                        {
                            break;
                        }


                        DeliverInfo delivermodel = new Deliver().GetModel(did);
                        dal.AddOrderRecord(orderid, -1, delivermodel.Name, "配送员 点 接受订单");

                        sql = "UPDATE dbo.Deliver SET workstate  = 20 WHERE DataId = " + did;
                        sql += ";UPDATE dbo.OrderDeliver SET DeliveryTime = GETDATE() WHERE OrderId = '" + orderid + "'"; //设置开始 配送时间
                        WebUtility.excutesql(sql);

                        new Deliver().InitWorkState(did);

                        new OrderStep().Add(new OrderStepInfo()
                        {
                            stepcode = 40,
                            title = "配送员正在赶往商家",
                            subtitle = "骑士电话：" + delivermodel.Phone,
                            deliverid = did,
                            addtime = DateTime.Now,
                            orderid = orderid,
                            revevar1 = "",
                            revevar2 = ""

                        });


                    }
                    break;
                case 2:
                    {
                        if (order.OrderStatus == 2)
                        {
                            break;
                        }

                        sql = "UPDATE dbo.Deliver SET workstate  = 30 WHERE DataId = " + did;
                        WebUtility.excutesql(sql);
                    
                        DeliverInfo delivermodel = new Deliver().GetModel(did);

                        dal.AddOrderRecord(orderid, -1, delivermodel.Name, "配送员 点 去取货");


                        string delivernameAndPhone = " "+delivermodel.Name + "(" + delivermodel.Phone + ")";

                        if (order.TogoId == 0) //跑腿订单没有，商家名称
                        {
                            order.TogoName = "爆走跑腿";
                        }

                        Hangjing.WebCommon.SendMsg.SendOrderDeliver(order.OrderComm, delivernameAndPhone, order.TogoName, orderid, order.cityid);

                        new Deliver().InitWorkState(did);


                        new OrderStep().Add(new OrderStepInfo()
                        {
                            stepcode = 50,
                            title = "配送员已经取货",
                            subtitle = "请等待配送",
                            deliverid = did,
                            addtime = DateTime.Now,
                            orderid = orderid,
                            revevar1 = "",
                            revevar2 = ""

                        });

                    }
                    break;
                case 3:
                    {
                        if (order.OrderStatus == 3)
                        {
                            break;
                        }

                        //todo 为配送员添加记录

  
                        Hangjing.WebCommon.SendMsg.SendOrderComplete(order.OrderComm, "", order.TogoName, orderid, order.cityid);

                        new OrderServiceImpl().deliverComplete(orderid, did);
                        new Deliver().InitWorkState(did);
                        new OrderStep().Add(new OrderStepInfo()
                        {
                            stepcode = 60,
                            title = "已送达",
                            subtitle = "",
                            deliverid = did,
                            addtime = DateTime.Now,
                            orderid = orderid,
                            revevar1 = "",
                            revevar2 = ""

                        });


                    }

                    break;
                default:
                    break;
            }
        }
        else
        {
            Response.Write("{\"orderid\":\"" + orderid + "\",\"state\":\"0\",\"msg\":\"此订单已经被其他配送员接收了，您不能再操作了\"}");
        }


        Response.End();
    }
}
